
import UIKit

class UTBaseTabBarController: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.tabBar.barTintColor = UIColor.white
        self.tabBar.isTranslucent = false
        UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor(hexString: "#151B2F"), .font: UIFont.systemFont(ofSize: 10, weight: .medium)], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor(hexString: "#533DFF"), .font: UIFont.systemFont(ofSize: 10, weight: .medium)], for: .selected)
        setupChildsViewController()
    }
    
    fileprivate func setupChildsViewController() {
        
    }
    
    open func addChildController(_ childController: UIViewController, _ title: String, _ image: String, _ selectImage: String) {
        childController.tabBarItem = UITabBarItem()
        childController.tabBarItem.title = title
        childController.tabBarItem.image = UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: selectImage)?.withRenderingMode(.alwaysOriginal)
        let navigationController = UTBaseNavigationController(rootViewController: childController)
        self.addChild(navigationController)
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let index = tabBarController.viewControllers?.firstIndex(of: viewController), index == 1 {
            if let navController = UIApplication.topViewController()?.navigationController as? UINavigationController {
                return false
            }
            return false
        }
        return true
    }

}

